package com.yylending.las.personal.loan;

import android.text.TextUtils;

import com.yyfax.infrastructure.eventbus.EventAgent;
import com.yyfax.infrastructure.mvp.IView;
import com.yyfax.infrastructure.mvp.Presenter;
import com.yyfax.infrastructure.mvp.PresenterFactory;
import com.yyfax.infrastructure.network.entity.BaseResultEntity;

import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

/**
 * P:借据
 * Created by ying_he on 2017/6/8.<br>
 * Copyright  2017 www.yylending.com. All Rights Reserved.<br><br>
 */
public class DebtEvidencePresenter extends Presenter<DebtEvidenceModel,DebtEvidencePresenter.View> {

    private DebtEvidencePresenter.View mView;

    public static class Generator implements PresenterFactory<DebtEvidencePresenter> {
        @Override
        public DebtEvidencePresenter create() {
            return new DebtEvidencePresenter();
        }
    }

    @Override
    public void attach(DebtEvidencePresenter.View view) {
        mView = view;
        mModel = new DebtEvidenceModel();
        EventAgent.register(this);
    }

    @Override
    public void detach() {
        mView = null;
        mModel = null;
        EventAgent.unregister(this);
    }

    public interface View extends IView {
        void getDebtEvidenceSuccess(DebtEvidenceModel.debtEvidence debtEvidence);
        void getDebtEvidenceFailed(String tips);
    }

    public void getDebtEvidence(String loanNo, String loanType) {
        DebtEvidenceModel.RequestParams params = new DebtEvidenceModel.RequestParams();
        params.loanNo = loanNo;
        params.type = loanType;
        execute(params, FUNCTIONAL_CODE, true);
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void getResult(DebtEvidenceModel.debtEvidenceResult result) {
        if (BaseResultEntity.isResponseSuccess(result)) {
            if (result.getData() != null) {
                mView.getDebtEvidenceSuccess(result.getData());
            }else {
                mView.getDebtEvidenceFailed(result.getRetMsg());
            }
        } else {
            mView.getDebtEvidenceFailed(TextUtils.isEmpty(result.getRetMsg()) ? "获取数据失败,请重试." : result.getRetMsg());
        }
    }
}
